<template>
  <div class="userInfo">
    <el-row>
      <el-col :span="6">
        <div class="userImg">
          <el-upload class="avatar-uploader" action="http://localhost:8081/user/uploadUserImg" name="userImg" :data="{username:username}" :show-file-list="false" :on-success="handleAvatarSuccess" :on-remove="handleRemove" :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <span v-if="imageUrl" class="el-upload-action" @click.stop="handleRemove()">
              <i class="el-icon-delete"></i>
            </span>
            <i v-else class="el-icon-upload2 avatar-uploader-icon" stop></i>
          </el-upload>
        </div>
      </el-col>

      <el-col :span="18">
        <el-form label-width="80px" :model="userInfo">
          <el-form-item label="昵称" required>
            <el-input class="userInput" v-model="userInfo.name" placeholder="请输入昵称"></el-input>
          </el-form-item>

          <el-form-item label="介绍">
            <el-input class="userInput" type="textarea" :rows="3" placeholder="请输入简介" v-model="userInfo.introduce"></el-input>
          </el-form-item>

          <el-form-item label="性别" required>
            <el-radio v-model="userInfo.sex" label="男">男</el-radio>
            <el-radio v-model="userInfo.sex" label="女">女</el-radio>
            <el-radio v-model="userInfo.sex" label="保密">保密</el-radio>
          </el-form-item>

          <el-form-item label="生日">
            <el-select class="select1" v-model="userInfo.year" placeholder="请选择">
              <el-option v-for="item in userInfo.years" :key="item.value" :value="item.value">
              </el-option>
            </el-select>
            年
            <el-select class="select" v-model="userInfo.month" placeholder="请选择" @change="getDay()">
              <el-option v-for="item in userInfo.months" :key="item.value" :value="item.value">
              </el-option>
            </el-select>
            月
            <el-select class="select" v-model="userInfo.day" placeholder="请选择">
              <el-option v-for="item in userInfo.days" :key="item.value" :value="item.value">
              </el-option>
            </el-select>
            日
          </el-form-item>
          <el-button class="sub" type="primary" @click="saveUserInfo()">保存</el-button>
        </el-form>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { saveUserInfo, getUserInfo } from "../../network/user";

export default {
  name: "UserInfo",
  data() {
    return {
      username: "",
      userInfo: {
        name: '',
        introduce: '',
        sex: "保密",
        years: [{ value: '' }],
        months: [{ value: '' }],
        days: [{ value: '' }],
        year: '',
        month: '',
        day: '',
      },
      imageUrl: "",
    }
  },
  created() {
    this.getYear();
    this.getMonth();
  },
  mounted() {
    this.getUserInfo();
  },
  methods: {
    getUserInfo() {
      this.username = localStorage.getItem("username");
      getUserInfo(localStorage.getItem("username")).then(res => {
        //console.log(res.data);
        if (res.data != null) {
          this.userInfo.name = res.data.nickname;
          this.userInfo.introduce = res.data.introduce;
          this.userInfo.sex = res.data.sex;
          this.userInfo.year = res.data.birthday.slice(0, 4);
          this.userInfo.month = res.data.birthday.slice(5, 7);
          this.userInfo.day = res.data.birthday.slice(8, 10);
          if (res.data.userImgName != null) {
            this.imageUrl = res.data.virPath + "/" + res.data.userImgName;
          }
        }
      })
    },
    getYear() {
      let year = new Date().getFullYear();
      let years = [];
      for (let i = year; i >= 1920; i--) {
        years.push({ value: i });
      }
      this.userInfo.years = years;
    },
    getMonth() {
      let months = [];
      for (let i = 1; i <= 12; i++) {
        months.push(i < 10 ? { value: '0' + i } : { value: i });
      }
      this.userInfo.months = months;
    },
    getDay() {
      let days = [];
      let date;
      if (this.userInfo.month == '01' || this.userInfo.month == '03' || this.userInfo.month == '05' || this.userInfo.month == '07' || this.userInfo.month == '08' || this.userInfo.month == 10 || this.userInfo.month == 12) {
        date = 31;
      } else if (this.userInfo.month == '02') {
        if (this.userInfo.year % 4 == 0 && this.userInfo.year % 100 != 0 || this.userInfo.year % 400 == 0) {
          date = 29;
        } else {
          date = 28;
        }
      } else {
        date = 30;
      }
      for (let i = 1; i <= date; i++) {
        days.push(i < 10 ? { value: '0' + i } : { value: i });
      }
      this.userInfo.days = days;
    },
    saveUserInfo() {
      saveUserInfo(localStorage.getItem("username"), this.userInfo.name, this.userInfo.introduce, this.userInfo.sex, this.userInfo.year + "年" + this.userInfo.month + "月" + this.userInfo.day + "日").then(res => {
        if (res.data == "200") {
          alert("保存成功");
        }
      })
    },
    // 移除图片
    handleRemove() {
      this.imageUrl = ''
    },
    // 上传成功回调
    handleAvatarSuccess(file) {
      //console.log(file);
      this.imageUrl = file;
      location.reload();
    },
    // 上传前格式和图片大小限制
    beforeAvatarUpload(file) {
      const type = file.type === 'image/jpeg' || 'image/jpg' || 'image/webp' || 'image/png' || 'image/gif';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!type) {
        this.$message.error('图片格式不正确!(只能包含jpg，png，webp，JPEG,gif)')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return type && isLt2M
    },
  }
}
</script>

<style scoped>
.userInfo {
  width: 80%;
  height: 80%;
  margin: 2rem auto;
}
.userImg {
  margin: 2.2rem;
}
.userInput {
  width: 42%;
}
.select1 {
  width: 11.5%;
  margin-left: 10px;
  margin-right: 10px;
}
.select {
  width: 9.5%;
  margin-left: 10px;
  margin-right: 10px;
}
.sub {
  margin: 10px 0 0 30px;
}
.avatar-uploader {
  width: 12rem;
  height: 12rem;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background: url("../../assets/img/defaultHead.png") no-repeat;
  background-size: 100% 100%;
}
.avatar-uploader-icon {
  font-size: 0;
  color: #fff;
  width: 12rem;
  height: 12rem;
  line-height: 12rem;
  text-align: center;
}
.avatar-uploader-icon:hover {
  font-size: 28px;
  background-color: rgba(0, 0, 0, 0.3);
}
.avatar {
  position: relative;
  width: 12rem;
  height: 12rem;
  display: block;
}
.el-upload-action {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  width: 100%;
  height: 100%;
  font-size: 0;
  color: #fff;
  text-align: center;
  line-height: 12rem;
}
.el-upload-action:hover {
  font-size: 20px;
  background-color: #000;
  background-color: rgba(0, 0, 0, 0.3);
}
</style>